package com.itqf.service.impl;


import com.itqf.bean.User;
import com.itqf.mapper.UserMapper;
import com.itqf.service.UserService;
import com.itqf.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * projectName: SpringMVCTest02
 *
 * @author: 元
 * time: 2021/7/6 0006 22:05
 * description:
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public ResultVO findAll() {
        List<User> userList = userMapper.findAllUsers();
        ResultVO resultVO = null;
        if (userList != null && userList.size() > 0) {
            resultVO = ResultVO.OK(userList);
        } else {
            resultVO = ResultVO.FAIL("未查询到本系统下的用户信息！");
        }
        return resultVO;
    }

    @Override
    public ResultVO findById(int id) {
        User user = userMapper.queryById(id);
        ResultVO resultVO = null;
        if (user != null) {
            resultVO = ResultVO.OK(user);
        } else {
            resultVO = ResultVO.FAIL("未查询到该用户！！");
        }
        return resultVO;
    }

    @Override
    public ResultVO findUser(String account) {
        User user = userMapper.queryByAccount(account);
        ResultVO resultVO = null;
        if (user != null) {
            resultVO = ResultVO.OK(user);
        } else {
            resultVO = ResultVO.FAIL("未查询到该用户！！");
        }
        return resultVO;
    }


    @Override
    public ResultVO addUser(User user) {
        int rows = userMapper.insertUser(user);
        ResultVO resultVO = null;
        if (rows > 0) {
            resultVO = ResultVO.OK("数据插入成功！");
        } else {
            resultVO = ResultVO.FAIL("您的数据插入失败！！");
        }
        return resultVO;
    }

    @Override
    public ResultVO removeUser(int id) {
        int rows = userMapper.deleteById(id);
        ResultVO resultVO = null;
        if (rows > 0) {
            resultVO = ResultVO.OK("删除选定用户成功！");
        } else {
            resultVO = ResultVO.FAIL("您的删除操作失败！！");
        }
        return resultVO;
    }

    @Override
    public ResultVO updateUser(User user) {
        int rows = userMapper.updateUser(user);
        ResultVO resultVO = null;
        if (rows > 0) {
            resultVO = ResultVO.OK("更新用户信息成功！");
        } else {
            resultVO = ResultVO.FAIL("您的更新操作失败！！");
        }
        return resultVO;
    }
}
